import main
import filtering_transport
import common
import output
import hierarchical

import datetime
import numpy as np
import copy

import matplotlib.pyplot as plt
import matplotlib.dates as mpld
from collections import OrderedDict

train_dates = [datetime.datetime(2014, 9, 10, 0, 0), \
               datetime.datetime(2014, 9, 12, 0, 0), \
               datetime.datetime(2014, 9, 13, 0, 0), \
               datetime.datetime(2014, 9, 17, 0, 0), \
               datetime.datetime(2014, 9, 21, 0, 0)]
               
train_train_dates = {datetime.datetime(2014, 9, 9, 0, 0) : (range(47) + range(241,293) + range(363,366), [138, 140, 141]), \
               datetime.datetime(2014, 9, 10, 0, 0)  : (range(34) + range(179,201) + range(301,357), [117, 118, 119]), \
               datetime.datetime(2014, 9, 21, 0, 0)  : (range(40) + range(400,450), [172, 177, 178]), \
               datetime.datetime(2014, 10, 10, 0, 0) : (range(42) + range(327,390) , [100, 101, 248, 256, 259, 268, 272, 294, 304, 317]), \
               datetime.datetime(2014, 10, 22, 0, 0) : (range(48) + range(152,176) + range(248,280), [191, 196]), }
               
test_dates = [datetime.datetime(2014, 9, 19, 0, 0), \
              datetime.datetime(2014, 9, 20, 0, 0), \
              datetime.datetime(2014, 9, 21, 0, 0), \
              datetime.datetime(2014, 9, 22, 0, 0), \
              datetime.datetime(2014, 9, 23, 0, 0), \
              datetime.datetime(2014, 9, 24, 0, 0), \
              datetime.datetime(2014, 9, 25, 0, 0), \
              datetime.datetime(2014, 9, 26, 0, 0), \
              datetime.datetime(2014, 9, 27, 0, 0), \
              datetime.datetime(2014, 9, 28, 0, 0), ]

train_dates = [datetime.datetime(2014, 9, 9, 0, 0),
               datetime.datetime(2014, 9, 10, 0, 0),
               datetime.datetime(2014, 9, 11, 0, 0),
               datetime.datetime(2014, 9, 15, 0, 0),
               datetime.datetime(2014, 9, 16, 0, 0),
               datetime.datetime(2014, 9, 17, 0, 0),
               datetime.datetime(2014, 9, 18, 0, 0),
               datetime.datetime(2014, 9, 19, 0, 0),
               datetime.datetime(2014, 9, 21, 0, 0),
               datetime.datetime(2014, 9, 22, 0, 0),
               datetime.datetime(2014, 9, 23, 0, 0),
               datetime.datetime(2014, 9, 24, 0, 0),
               datetime.datetime(2014, 9, 25, 0, 0),
               datetime.datetime(2014, 9, 26, 0, 0),
               datetime.datetime(2014, 9, 29, 0, 0),
               datetime.datetime(2014, 9, 30, 0, 0),
               datetime.datetime(2014, 10, 1, 0, 0),
               datetime.datetime(2014, 10, 2, 0, 0),
               datetime.datetime(2014, 10, 3, 0, 0),
               datetime.datetime(2014, 10, 4, 0, 0),
               datetime.datetime(2014, 10, 5, 0, 0),
               datetime.datetime(2014, 10, 6, 0, 0),
               datetime.datetime(2014, 10, 7, 0, 0),
               datetime.datetime(2014, 10, 8, 0, 0),
               datetime.datetime(2014, 10, 9, 0, 0),
               datetime.datetime(2014, 10, 10, 0, 0),
               datetime.datetime(2014, 10, 21, 0, 0),
               datetime.datetime(2014, 10, 22, 0, 0)               
]
train_dates = [datetime.datetime(2014, 9, 10, 0, 0)]

#train_train_dates = { datetime.datetime(2014, 10, 10, 0, 0) : (range(42) + range(327,390) , [100, 101, 248, 256, 259, 268, 272, 294])}
               
all_results = OrderedDict()
window_length_list = [40]
min_window_list =  [20]

for window_length in window_length_list:
    for min_window in min_window_list:
        loc_res = []
        dsb_res = []
        trans_res = []
        for start_date in train_dates:
            # READ DATA
            end_date = start_date + datetime.timedelta(days = 1)
            [class_data, scan_data_binned, gps_data] = main.load_data(start_date, end_date)
                    
            location_dates = [datetime.datetime(2014, 9, 10, 7, 23), datetime.datetime(2014, 9, 10, 8, 9), datetime.datetime(2014, 9, 10, 15, 58), datetime.datetime(2014, 9, 10, 16, 23)]
            loc_coord = common.get_location_coordinates(gps_data, location_dates, start_date, end_date)
            
            for t_date_idx in xrange(0, len(location_dates), 2):
                start_transport = location_dates[t_date_idx]
                end_transport = location_dates[t_date_idx + 1]
            
            trans_routers = OrderedDict()
            for item in scan_data_binned.keys():
                if item in [('6c:f3:7f:0f:be:21', 'wireless2'), ('00:19:70:23:87:f1', 'TDC-A92C'), ('a4:b1:e9:af:2a:57', 'TelenorAF2A57'), ('2c:b0:5d:fc:54:6f', 'kvkdjhhh'), ('12:a0:96:c1:7b:26', 'DIRECT-cS-BRAVIA'), ('4c:60:de:79:5f:54', 'herskabet'), ('7c:03:4c:d9:f8:a0', 'HomeBox-F89A'), ('9c:97:26:6e:7f:55', 'TeliaGateway9C-97-26-6E-7F-55'), ('4e:03:4c:c7:db:f7', 'wlanhome'), ('08:76:ff:c6:9f:10', 'TeliaGateway08-76-FF-C6-9F-10'), ('94:44:52:8f:23:90', 'Farmors_netvaerk'), ('7c:03:4c:c7:db:f7', 'LC'), ('c0:4a:00:f3:34:16', 'Kop2kaffe (5GHz)'), ('10:9a:dd:87:54:28', 'JBT_Westend'), ('7c:03:d8:c8:fb:2c', 'HomeBox-FB26'), ('74:44:01:f4:0c:9f', 'bdzkbbtd'), ('08:76:ff:49:06:f7', 'TELIA-5A6154'), ('44:d8:84:6b:3d:a7', 'PR Wi-Fi-netv\xc3\xa6rk'), ('04:a1:51:19:7e:44', 'DanesAir'), ('00:1e:2a:06:66:5c', 'wst'), ('7c:03:4c:da:5c:79', 'HomeBox-5C73'), ('00:0f:b5:db:03:a4', 'BOKSEN'), ('20:4e:7f:45:76:04', 'SOBA'), ('58:6d:8f:36:8d:84', 'Carlsen 2.4ghz'), ('f4:dc:f9:c8:b9:3c', 'Lambert'), ('7c:05:07:5c:0d:d9', 'Nannan'), ('10:0d:7f:ca:13:a0', 'amydqa3g'), ('58:6d:8f:36:8d:85', 'Carlsen 5ghz'), ('00:19:70:1c:5c:fb', 'TDC-D7D8'), ('08:60:6e:21:06:f8', 'Zeppelin'), ('e8:94:f6:94:a4:5e', 'Cafe Phenix'), ('0a:60:6e:21:06:f9', 'Zeppelin Guest'), ('00:23:f8:30:00:2e', 'ZyXEL'), ('08:76:ff:48:f8:18', 'TELIA-2F460C'), ('00:19:70:36:59:10', 'TDC-A424'), ('dc:9f:db:b2:90:40', 'LeLe'), ('c2:9f:db:b2:90:40', 'LeLe Guest'), ('00:26:18:90:28:b2', 'xbNngF7R'), ('7c:03:4c:d7:75:fe', 'Caf\xc3\xa9 Obelix'), ('0a:27:22:b5:99:95', 'LeLe Guest'), ('c4:3d:c7:32:1f:ee', 'kJ5byP3g'), ('64:66:b3:6f:8d:66', 'fiberby.dk-50UZGC'), ('06:18:e7:12:7d:dc', 'HOTSPOT'), ('d6:ca:6d:c9:6c:c4', ''), ('00:15:63:9a:5d:f0', ''), ('00:16:46:f8:50:a1', 'WLAN_DSB'), ('06:18:0a:d8:73:a0', 'tablet'), ('00:0b:6b:23:4e:ad', 'AIObserver'), ('00:18:0a:d8:73:a0', 'D25'), ('00:19:70:33:8f:b2', 'TDC-2754'), ('00:19:70:68:12:1d', 'TDC-6646'), ('a4:4c:11:6f:d2:10', 'BakeKontor'), ('a4:4c:11:6f:d2:11', 'Lagkagehuset'), ('00:0e:8e:1a:ad:39', 'ZetaDisplay'), ('00:16:46:f8:50:a0', 'DSB_WLAN'), ('c8:b3:73:04:2a:b8', 'WLAN Zone - The Cloud'), ('ca:b3:73:04:2a:b9', 'UPPER CRUST WiFi'), ('d6:ca:6d:c9:6c:c3', 'McDonalds Gratis Wi-Fi'), ('00:1e:4a:90:94:c1', 'WLAN_DSB'), ('00:1b:2b:37:72:e0', 'FRTOG_DSB'), ('a4:b1:e9:ed:e1:44', 'TelenorEDE144'), ('00:1b:2b:37:76:10', 'FRTOG_DSB'), ('00:1e:4a:90:94:c3', 'DSB_WLAN'), ('00:1b:2b:37:72:e3', 'DSB_WLAN'), ('00:1b:2b:37:76:13', 'DSB_WLAN'), ('00:1b:2b:37:76:12', 'FR-TOG'), ('00:21:d7:93:a9:90', 'TDC'), ('a4:56:30:cc:2f:90', 'LPOS'), ('00:1b:2b:37:76:11', 'WLAN_DSB'), ('00:1e:4a:90:94:c0', 'FRTOG_DSB'), ('00:1e:4a:90:94:c2', 'FR-TOG'), ('00:40:10:10:00:03', 'oznog'), ('5c:0e:8b:d0:09:90', 'MICROS'), ('5c:0e:8b:d0:09:91', 'Paytable'), ('c2:97:c6:80:cb:fe', 'SETUP'), ('50:2e:5c:f1:89:ac', 'anetwork2'), ('9c:d6:43:00:01:d9', 'Kapacitet'), ('6c:70:9f:da:2b:14', 'gerson33'), ('00:19:70:83:f9:6b', 'LEXyZ'), ('04:a1:51:2e:c1:a2', 'bzvv5xby'), ('00:19:70:78:b7:de', 'TDC-ED44'), ('8a:43:e1:ac:14:34', 'SPEJDERGUEST'), ('a4:b1:e9:f1:59:04', 'TelenorF15904'), ('00:1d:aa:ae:8b:48', 'FOF Jernbanepladsen'), ('00:1c:57:e0:a1:03', 'LTKMOBILE'), ('00:1c:57:e0:a1:00', 'LTKAIR'), ('00:1c:57:e0:9f:92', ''), ('00:1c:57:e0:a1:02', ''), ('28:3c:e4:3d:7f:f0', 'WLAN1-A23C0E'), ('00:1c:57:e0:9f:93', 'LTKMOBILE'), ('00:1c:57:e0:9f:90', 'LTKAIR'), ('00:1c:57:e0:9f:9f', 'LTKAIR'), ('00:1c:57:e0:9f:9c', 'LTKMOBILE'), ('44:94:fc:f0:94:96', '3Bredband9496'), ('00:19:70:75:65:8f', 'TDC-AAEC'), ('b2:b2:dc:9c:64:4c', 'Speedworks'), ('00:1c:57:e0:9f:91', ''), ('a0:21:b7:d8:11:80', 'XPMVjDvu'), ('7c:03:d8:ca:4a:ca', 'HomeBox-4AC4'), ('00:1c:57:e0:a1:0e', ''), ('00:1c:57:e0:a1:0c', 'LTKMOBILE'), ('00:1c:57:e0:a1:0f', 'LTKAIR'), ('7c:03:d8:ca:2f:6b', 'HomeBox-2F65'), ('88:43:e1:ac:14:33', ''), ('00:1c:57:e0:a1:01', ''), ('7c:03:4c:dc:35:8e', 'HomeBox-3588'), ('00:26:44:04:2c:db', 'TELIA-0236E2'), ('7c:03:d8:cd:51:80', 'HomeBox-517A'), ('00:1c:57:e0:b0:83', 'LTKMOBILE'), ('00:1c:57:e0:b0:80', 'LTKAIR'), ('20:e5:2a:f4:78:91', 'MbDbuVMH'), ('64:70:02:d1:9a:b3', 'SKL'), ('a4:b1:e9:f2:bd:00', 'TelenorF2BD00'), ('44:94:fc:e2:3d:9a', '3Bredband3D9A'), ('f4:dc:f9:c8:a6:db', 'HUAWEI-E5172-A6DB'), ('00:19:70:69:37:1d', 'TDC-C4BE'), ('00:19:70:68:1a:85', 'TDC-133C'), ('c0:c1:c0:59:cc:f1', 'Th Sek'), ('00:19:70:5c:73:78', 'bhv4'), ('2c:b0:5d:c6:48:50', 'Radioport'), ('7c:03:d8:ca:ec:06', 'HomeBox-EC00'), ('00:23:54:24:fc:53', '7Xx7sfmt'), ('58:98:35:b5:b1:13', 'TeliaGateway58-98-35-B5-B1-13'), ('04:a1:51:31:f3:b0', 'Mark'), ('a4:b1:e9:ee:93:d8', 'TeliaGatewayA4-B1-E9-EE-93-D8'), ('00:22:3f:80:22:de', 'JB Net'), ('00:8e:f2:b5:56:6d', '53a3yyu7'), ('00:19:70:1c:d4:76', 'TDC-D140'), ('00:1b:fc:08:f2:0a', 'jRt3Mujy'), ('7c:03:4c:cc:af:db', 'HomeBox-AFD5'), ('00:1c:df:b6:06:e1', 'belkinNR1'), ('00:19:70:64:d0:88', 'TDC-B0D4'), ('b8:be:bf:b7:ed:a2', 'Topsoe-ext'), ('00:19:70:6c:32:ad', 'TDC-87A0'), ('68:86:a7:53:b7:ef', 'golderwlan'), ('68:86:a7:53:b7:ee', ''), ('68:86:a7:53:f9:ff', 'golderwlan'), ('68:86:a7:53:f9:fe', ''), ('68:86:a7:53:b7:ed', 'golderguest'), ('68:86:a7:53:f9:fd', 'golderguest'), ('68:86:a7:53:f9:f1', ''), ('90:72:40:13:15:b9', 'Stigs Wi-Fi-netv\xc3\xa6rk 5 GHz'), ('00:1d:7e:ec:5d:8b', 'Donaco'), ('d4:c9:ef:54:ca:a9', 'HP-Print-A9-Photosmart 6520'), ('b8:a3:86:52:d1:15', 'dlink'), ('c8:d7:19:da:3c:aa', 'Linksys_AJT'), ('18:64:72:ee:28:61', 'GEO2014'), ('18:64:72:ee:a4:61', 'GEO2014'), ('08:63:61:bc:27:34', 'MD_vaerksted'), ('00:19:70:60:f1:c1', 'TDC-98BC'), ('c8:d7:19:da:3c:a9', 'Linksys_AJT5'), ('18:64:72:ee:a4:62', 'GEO-Guest'), ('18:64:72:ee:28:62', 'GEO-Guest'), ('00:19:70:56:cb:02', 'AP08F1CB'), ('00:19:70:98:bc:f7', 'TDC-6F8C'), ('00:1b:8f:88:c4:d6', 'Development'), ('90:4e:2b:13:a3:e6', 'GEORIG'), ('00:1b:8f:88:c4:d5', ''), ('00:1b:8f:88:c4:d9', 'Hardware'), ('00:1b:8f:88:c4:de', 'Mark Info'), ('00:1b:8f:88:c4:db', 'Management'), ('00:1b:8f:88:c4:d4', ''), ('c4:05:28:64:d1:3e', 'HUAWEI-E5172-D13E'), ('00:1b:8f:88:c4:d8', ''), ('52:67:f0:f9:02:24', 'TelenorVJF3'), ('00:1b:8f:88:ae:f9', 'Hardware'), ('00:1b:8f:88:c4:df', ''), ('00:1b:8f:88:c4:d9', 'Development'), ('00:1b:8f:88:c4:d7', ''), ('00:1b:8f:88:ae:f0', ''), ('00:1b:8f:88:c4:d6', 'Hardware'), ('00:1b:8f:88:c4:d1', 'Mark Info'), ('00:1b:8f:88:c4:dc', ''), ('00:1b:8f:88:c4:d4', 'Management'), ('00:0f:b5:a1:b0:4c', 'BTF'), ('46:a7:4a:7e:a7:e2', 'Portthru'), ('a0:21:b7:ff:28:d8', 'hUKrs634'), ('7c:03:d8:cf:cd:79', 'HomeBox-CD73'), ('60:33:4b:e4:6c:e5', 'peterhjem'), ('00:16:b6:5b:f1:2f', 'DV58B'), ('00:19:70:60:f1:40', 'TDC-9778'), ('00:19:70:80:30:25', 'TDC-ED68'), ('00:25:4b:03:fa:bd', 'Morten & Tanja'), ('00:1f:f3:c0:fa:cb', 'Morten & Tanja'), ('c0:4a:00:37:03:98', 'TDC-854C'), ('10:0d:7f:aa:18:39', 'Oles fine guest wifi'), ('10:0d:7f:aa:18:38', 'The Promised Lan'), ('00:21:91:38:7a:4c', 'Totalforvirring'), ('00:0c:f6:4a:9a:46', 'Sitecom4A9A46'), ('00:3a:98:bf:d7:f3', 'MED-105'), ('78:54:2e:fe:fa:5c', 'DonCorleone'), ('00:3a:98:bf:d7:f1', 'PDA-105'), ('00:3a:98:bf:d7:f2', 'KNS-105'), ('c8:d3:a3:6d:a7:51', 'monicahannani'), ('00:18:f8:57:1d:f1', 'OPEN1'), ('00:1e:13:1b:8e:3f', 'eduroam'), ('68:7f:74:17:e3:84', 'slow'), ('e0:3f:49:f2:4b:00', 'TBargumI'), ('bc:ee:7b:65:d3:3c', 'Jens'), ('00:0f:61:b5:72:50', 'K-Net'), ('c8:d3:a3:07:7e:a0', 'LAHJ_WLAN'), ('00:25:00:40:d3:b0', '811'), ('00:3a:98:bf:d7:f0', 'Netto Hotspot'), ('00:19:cb:7b:96:5e', 'Room 9'), ('00:1e:13:1b:8e:31', 'device'), ('00:1e:13:1b:8e:33', 'dtu'), ('00:1e:13:1b:8e:3e', 'device'), ('00:1e:13:1b:8e:3d', 'eksamen'), ('00:1e:13:1b:8e:3c', 'dtu'), ('00:3a:98:c9:b5:02', 'KNS-105'), ('c8:d3:a3:6d:a7:53', 'monicahannani'), ('00:3a:98:c9:b5:01', 'PDA-105'), ('00:3a:98:c9:b5:03', 'MED-105'), ('a0:d3:c1:ac:17:30', 'K-Net'), ('78:54:2e:fe:fa:5e', 'TheGodfather'), ('a0:d3:c1:ac:17:20', 'K-Net'), ('00:22:b0:b3:f1:34', 'dlink'), ('00:3a:98:c9:b5:00', 'Netto Hotspot'), ('00:1e:13:1b:8e:30', 'eduroam'), ('e0:91:f5:84:1f:7e', 'NETGEAR'), ('a0:d3:c1:a3:b8:50', 'K-Net'), ('74:44:01:89:b7:b3', 'NETGEAR02'), ('00:26:f2:00:d2:0e', 'Mong'), ('e8:94:f6:aa:f9:fc', 'Sebs'), ('bc:ee:7b:55:c5:58', '0301'), ('00:0f:b5:a5:3f:1a', 'Genesis'), ('d8:30:62:32:8b:d3', 'MWLAN'), ('00:14:bf:e1:ca:e9', 'Wax On Wax Off'), ('84:1b:5e:df:58:ae', 'NETGEAR95'), ('00:1c:df:02:9a:9c', 'Belkin_G_Plus_MIMO_029A9C'), ('00:1e:13:1b:8e:32', 'eksamen'), ('2c:b0:5d:4a:9f:0e', 'Am0410'), ('00:1d:7e:bc:42:85', 'FBI Surveillance Van'), ('34:21:09:00:f4:c4', 'AirLink89300'), ('10:c3:7b:a4:03:d8', '313'), ('44:94:fc:84:b8:18', 'NETGEAR312'), ('d8:50:e6:ae:2f:50', 'Haters gonna hate 2'), ('10:0d:7f:47:c2:86', 'KOEGENET'), ('c0:3f:0e:7b:68:b3', 'Jeppe'), ('d8:50:e6:ae:2f:54', 'Haters gonna hate 2'), ('00:21:29:70:0e:f1', 'linksys'), ('bc:ee:7b:ef:32:6a', 'Chupamela'), ('4c:60:de:cd:8b:a0', 'ehhh det er en hjort'), ('20:aa:4b:88:6d:4e', 'NizzleYo'), ('10:fe:ed:e6:f4:f6', 'TP-LINK_E6F4F6'), ('00:19:cb:59:77:56', 'pallemusen'), ('cc:5d:4e:29:dd:48', '2510'), ('4c:60:de:3d:62:70', 'Please wait...'), ('c8:60:00:94:0f:71', 'mongol'), ('90:f6:52:f2:8f:ba', 'Solsikkeso 2'), ('00:22:6b:6f:83:dc', 'snet'), ('a0:f3:c1:82:0a:3a', '2709'), ('c8:d3:a3:04:fe:a4', 'A.K._D-Link'), ('00:1b:11:ce:c5:34', 'MajaErNice'), ('bc:ee:7b:56:75:38', 'Gvaleur'), ('00:1c:10:a9:2c:d1', 'dd-wrt'), ('00:24:a5:bc:48:02', 'giraffen'), ('00:15:e9:60:d9:9c', 'Tauby'), ('00:1e:58:bb:6c:97', 'dlink'), ('a0:d3:c1:a3:be:f0', 'K-Net')]:
                    trans_routers[item] = scan_data_binned[item]
                    
            print "Transportation:"
            for loc_idx in xrange(len(loc_coord) - 1):
                trans_start_time = location_dates[loc_idx*2]
                trans_end_time = location_dates[loc_idx*2 + 1]
                speed_data = common.get_transportation_speed(gps_data, trans_start_time, trans_end_time)
                output.plot_speed(speed_data, trans_start_time, trans_end_time, "speed_" + datetime.datetime.strftime(trans_start_time, "%Y-%m-%d-%H-%M") + datetime.datetime.strftime(trans_end_time, "%Y-%m-%d-%H-%M") + ".pdf", (8,4))
                distance = common.distance_on_unit_sphere(loc_coord.values()[loc_idx], loc_coord.values()[loc_idx + 1])
                time = (trans_end_time - trans_start_time).seconds / float(60)
                if time > 0:
                    speed = common.calculate_speed(distance, time)
                    [avg_router_life, max_router_life, new_routers] = common.router_life(trans_routers, trans_start_time, trans_end_time, start_date, end_date)
                    output.append_to_file((trans_start_time, trans_end_time, distance, time, speed, avg_router_life, max_router_life, new_routers))
                    print trans_start_time, trans_end_time, distance, time, speed, avg_router_life, max_router_life, new_routers
                    output.plot_router_trans_matrix(trans_routers, class_data, trans_start_time, trans_end_time, "transportation_routers_" + datetime.datetime.strftime(trans_start_time, "%Y-%m-%d-%H-%M") + datetime.datetime.strftime(trans_end_time, "%Y-%m-%d-%H-%M") + ".pdf", (10,10), False, speed_data, {}, False)
#                output.plot_router_trans_matrix(scan_data_binned_orig, class_data, trans_start_time, trans_end_time, "orig_routers_" + datetime.datetime.strftime(trans_start_time, "%Y-%m-%d-%H-%M") + datetime.datetime.strftime(trans_end_time, "%Y-%m-%d-%H-%M") + ".pdf", (10,10), False, {}, {}, True)
                
#        all_results[threshold] = (np.mean(loc_res), np.mean(dsb_res), np.mean(trans_res))
   
#plt.plot(all_results.keys(), [item[0] for item in all_results.values()], c=output.my_colors[0], label="Location routers")
#plt.plot(all_results.keys(), [item[1] for item in all_results.values()], c=output.my_colors[1], label="Mobile routers")
#plt.plot(all_results.keys(), [item[2] for item in all_results.values()], c=output.my_colors[2], label="Transportation routers")
#plt.ylim([0,1])
#plt.legend()
#plt.show()
